﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Data.SqlClient;

namespace LibraryManager
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private string _id;// 账号
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;
        public string Name1 // 姓名
        {
            get { return _name; }
            set { _name = value; }
        }

        // 权限标记 值为admin为管理员 ，值为user则为普通用户
        private string _tag;
        public string _Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }

        // 标记账户是否被锁定  1为锁定 
        private string _b;  
        public string B
        {
            get { return _b; }
            set { _b = value; }
        }

        // 在加载窗体时设定操作权限
        private void MainForm_Load(object sender, EventArgs e)
        {

            if (_Tag == "user")
            {
                tSSL2.Text = "权限级别：普通用户";
                tSSL5.Text = "   " + Name1;

                NewUserRegister.Enabled = false;
                LockUser.Enabled = false;
                NewBooksInStorage.Enabled = false;
                BorrowBook.Enabled = false;
                ReturnBook.Enabled = false;
                LogoutBook.Enabled = false;
                FindUserDetail.Enabled = true;
                AddUser.Enabled = false;
                AddBook.Enabled = false;
                BorrowBookTodo.Enabled = false;
                ReturnBookTodo.Enabled = false;
                DeleteUser.Enabled = false;

                if (B == "1")
                {
                    toolStripStatusLabel3.Text = " 状态：被锁定";
                    MessageBox.Show("账户被锁定，请尽快联系管理员", "警告");
                }
            }
            else
            {
                tSSL2.Text = "权限级别：管理员";
                tSSL5.Text = "   " + Name1;
            } 
        }

        #region 关于、帮助
        private void AboutSoftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("学号：8002117349\n姓名：欧阳暄林\n系统：图书管理系统", "关于");
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            MessageBox.Show("基于WinForm的图书管理系统", "开发环境");
        }
        #endregion

        // 时钟
        private void timer1_Tick(object sender, EventArgs e)
        {
            time.Text = DateTime.Now.ToString();
        }

        // 退出时关闭所有窗口（login）
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit(); 
        }

        #region 修改密码
        private void ChangePassWord_Click(object sender, EventArgs e)
        {
            AlterPwdForm altPF = new AlterPwdForm();
            altPF.Id = Id;
            altPF._Tag = _Tag;
            altPF.ShowDialog();
        }
        #endregion

        #region 修改个人信息
        private void ChangeDetail_Click(object sender, EventArgs e)
        {
            UpdateIndivForm uIF = new UpdateIndivForm();
            uIF.Id = Id;
            uIF._Tag = _Tag;
            uIF.ShowDialog();
        }
        #endregion

        #region 添加用户
        private void AddUser_Click(object sender, EventArgs e)
        {
            UpdateIndivForm uIF = new UpdateIndivForm();
            uIF.Id = Id;
            uIF._Tag = _Tag;
            uIF.P = "1";
            uIF.Text = "新用户注册";
            uIF.ShowDialog();
        }
        #endregion

        #region 删除用户
        private void DeleteUser_Click(object sender, EventArgs e)
        {
            UpdateIndivForm deleteForm = new UpdateIndivForm();
            deleteForm.Text = "删除用户";
            deleteForm.P = "2";
            deleteForm.Id = Id;
            deleteForm._Tag = _Tag;
            deleteForm.ShowDialog();
        }
        #endregion

        #region 添加书籍
        private void AddBook_Click(object sender, EventArgs e)
        {
            InsertNewBookForm iNB = new InsertNewBookForm();
            iNB.ShowDialog();
        }
        #endregion

        #region 书籍注销
        private void LogoutBook_Click(object sender, EventArgs e)
        {
            InsertNewBookForm iNB = new InsertNewBookForm();
            iNB.P = "1";
            iNB.Text = "注销书籍";
            iNB.ShowDialog();
        }
        #endregion

        #region 书籍查询
        private void FindBook_Click(object sender, EventArgs e)
        {
            FindBookForm fbf = new FindBookForm();
            fbf.Tag1 = this._Tag;
            fbf.ShowDialog();
        }
        #endregion

        #region 借书办理
        private void BorrowBook_Click(object sender, EventArgs e)
        {
            BorrowBooks bb = new BorrowBooks();
            bb.ShowDialog();
        }        
        #endregion

        #region 还书办理
        private void RetuenBook_Click(object sender, EventArgs e)
        {
            ReturnBooks rb = new ReturnBooks();
            rb.ShowDialog();
        }        
        #endregion

        #region 借书单查询
        private void FindRecord_Click(object sender, EventArgs e)
        {
            FindRecordForm fb = new FindRecordForm();
            fb.ID = _id;
            fb.NAME = _name;
            fb._Tag = _Tag;
            fb.ShowDialog();
        }
        #endregion

        #region 查询用户信息
        private void FindUserDetail_Click(object sender, EventArgs e)
        {
            if(_Tag == "user")
            {
                FindUserDetailForm fdu = new FindUserDetailForm();
                fdu.Id = Id;
                fdu.Tag = Tag;
                fdu.ShowDialog();
            }
            else
            {
                UserDetailForm ud = new UserDetailForm();
                ud.ShowDialog();
            }
        }
        #endregion

        #region 退出当前用户
        private void LogoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定退出当前用户吗？", "注销", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                this.Hide();
                LoginForm login = new LoginForm();
                Id = "";
                Name1 = "";
                login.Show();
            }
            else { return; }
        }
        #endregion

        #region 锁定用户处理
        private void LockUser_Click(object sender, EventArgs e)
        {
            LockUserForm lu = new LockUserForm();
            lu.ShowDialog();
        }
        #endregion
    }
}
